﻿using System;
using System.Data;

namespace DBBuilder.DBDataStructure
{
    /// <summary>
    /// Present data type corrresponding with data type from DataBase
    /// </summary>
    public enum DataType
    {
        INT,
        STRING,
        GUILD,
        DATETIME,
        BOOL,
        FLOAT,
        DOUBLE,
        LONG,
        IMAGE
    }

    /// <summary>
    /// Present attribute corresponding with attribute of table from database
    /// </summary>
    public class Attribute
    {
        #region Fields

        /// <summary>
        /// Attribute Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Present data type corrresponding with data type from DataBase
        /// </summary>
        public DataType Type { get; set; }

        /// <summary>
        /// Used for determine whether attribute is primary key of a table
        /// </summary>
        public bool IsPrimaryKey { get; set; }

        /// <summary>
        /// Used for determine whether attribute is foreign key of a table
        /// </summary>
        public bool IsForeignKey { get; set; }

        /// <summary>
        /// If the attribute is foreign key, this variable is used for determine which table that attribute refer to
        /// </summary>
        public string ReferTo { get; set; }

        /// <summary>
        /// If the attribute is primary key, this variable is used for determine whether attribute is identify
        /// </summary>
        public bool IsIdentify { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initialize instance of attribute based on DataColumn input
        /// </summary>
        /// <param name="col"></param>
        public Attribute(DataColumn col)
        {
            this.Name = col.ColumnName;
            this.Type = GetType(col.DataType);
        }

        #endregion

        #region Methods

        /// <summary>
        /// From base type which is corresponding with DB, execute a convert to customize DataType
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private DataType GetType(Type type)
        {
            switch (type.ToString())
            {
                case "System.String":
                    return DataType.STRING;

                case "System.Float":
                    return DataType.FLOAT;

                case "System.Double":
                    return DataType.DOUBLE;

                case "System.Int32":
                    return DataType.INT;

                case "System.Int64":
                    return DataType.LONG;

                case "System.Guid":
                    return DataType.GUILD;

                case "System.Boolean":
                    return DataType.BOOL;

                case "System.DateTime":
                    return DataType.DATETIME;

                default:
                    return DataType.STRING;
            }
        }

        #endregion
    }
}
